package com.jiai.cloud.community.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jiai.cloud.base.entity.RestResult;
import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.page.CustomPage;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.base.util.SpringUtil;
import com.jiai.cloud.community.entity.TSystemCatalog;
import com.jiai.cloud.community.entity.TSystemImg;
import com.jiai.cloud.community.service.ITSystemCatalogService;
import com.jiai.cloud.community.service.ITSystemImgService;
import com.jiai.cloud.config.Config;
import com.jiai.cloud.utils.UploadUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.List;


/**
 * Created by Administrator on 2017/6/30.
 */
@RestController
@RequestMapping("/api/v1/TSystemImg")
public class TSystemImgController {

    private Logger logger= LoggerFactory.getLogger(FileContoller.class);
    @Autowired
    private ITSystemCatalogService itSystemCatalogService;
    @Autowired
    private ITSystemImgService iTSystemImgService;

    @RequestMapping(value = "/upload/{systemCatalogId}",method = RequestMethod.POST)
    @ResponseBody
    public RestResult upload(@RequestParam("file") MultipartFile file,@PathVariable("systemCatalogId") Integer systemCatalogId){
        try {
            TSystemCatalog tSystemCatalog=itSystemCatalogService.selectById(systemCatalogId);
            String filePath =tSystemCatalog.getSystemsaveCatalog();
            String pate=UploadUtil.copyFile(file,filePath);
            TSystemImg tSystemImg=new TSystemImg();
            tSystemImg.setSystemCatalogId(systemCatalogId);
            tSystemImg.setImgUrl(filePath+"/"+pate);
            Integer cunt=(int) file.getSize();
            tSystemImg.setSize(cunt);
            tSystemImg.setCreateTime(new Date());
            iTSystemImgService.insert(tSystemImg);
            return RestResultGenerator.genSuccessResult(tSystemImg);
        }catch (Exception e){
            logger.error("文件上传失败!"+e.getMessage());
            return RestResultGenerator.genErrorResult("上传失败");
        }
    }
    //查询所有图片
    @RequestMapping(value = "/findByAll",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    public Object findByAll(@RequestParam("page") Integer page) throws ServiceException {
        try {
            Page<TSystemImg> pageSize= new Page<>(page, 10);
            iTSystemImgService.selectPage(pageSize, null);
            return RestResultGenerator.genSuccessResult(new CustomPage(pageSize));
        }catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }
    //通过分类id来查处所有图片
    @RequestMapping(value = "/findById", method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public Object findById(@RequestParam("page") Integer page,@RequestParam("systemCatalogId") Integer systemCatalogId) throws ServiceException {
        try {
            Page<TSystemImg> pageSize= new Page<>(page, 10);
            iTSystemImgService.selectPage(pageSize, new EntityWrapper<TSystemImg>().eq("systemCatalogId",systemCatalogId));
            return RestResultGenerator.genSuccessResult(new CustomPage(pageSize));
        } catch (Exception e) {
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }
    @RequestMapping(value="/delete", produces = {"application/json;charset=UTF-8"},method = RequestMethod.POST)
    public Object delete( Integer id,Integer systemCatalogId) throws ServiceException {
        try {
            String filePath = SpringUtil.getBean(Config.class).getFilePath();
            TSystemImg tSystemImg =iTSystemImgService.selectById(id);
            String pata=filePath+"/"+tSystemImg.getImgUrl();
            File file = new File(pata);
            if (file.isFile() && file.exists()){
                iTSystemImgService.deleteById(id);
                file.delete();
                return RestResultGenerator.genSuccessResult(systemCatalogId);
            }else{
                return RestResultGenerator.genErrorResult("文件删除失败");
            }
        }catch (Exception e){
            throw new ServiceException("删除失敗",e.getMessage());
        }
    }
    //前台显示
    @RequestMapping(value = "/thefindByAll",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    public Object thefindByAll() throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(iTSystemImgService.selectList(null));
        }catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }
    //前台显示
    @RequestMapping(value = "/thefindById", method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public Object thefindById( @RequestParam("systemCatalogId") Integer systemCatalogId) throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(iTSystemImgService.selectList(new EntityWrapper<TSystemImg>().eq("systemCatalogId",systemCatalogId)));
        } catch (Exception e) {
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }

}
